package com.fortune.service.impl;

import com.fortune.dao.DStoreDeficitDao;
import com.fortune.dao.DStoreSurplusDao;
import com.fortune.dto.in.dStoreDeficit.DStoreDeficitAddDto;
import com.fortune.dto.in.dStoreDeficit.DStoreDeficitEditDto;
import com.fortune.dto.in.dStoreDeficit.DStoreDeficitInfoDto;
import com.fortune.dto.in.dStoreDeficit.DStoreDeficitListDto;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.DStoreDeficitService;
import com.fortune.vo.dStoreDeficit.DStoreDeficitInfo;
import com.fortune.vo.dStoreDeficit.DStoreDeficitInfoDetail;
import com.fortune.vo.dStoreDeficit.DStoreDeficitInfoList;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfo;
import com.fortune.vo.mStoreDeficit.StockDetailDeficit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
public class DStoreDeficitServiceImpl implements DStoreDeficitService {
    @Autowired
    private DStoreDeficitDao dStoreDeficitDao;



    @Override
    @Transactional
    public int  addDeficitBase( String dealerId, String uid, DStoreDeficitAddDto addDto) {
        int num = dStoreDeficitDao.findNumIsNull(addDto.getNum());
        if(num > 0){
            return -1;
        }
        String uuid= UUID.randomUUID().toString().replace("-", "");
        int addDeficitBase=dStoreDeficitDao.addDeficitBase(dealerId,uuid,uid,addDto);
        if(addDeficitBase>0){
            int addDeficitDetail=0;
            for(DStoreDeficitInfoDto infoDto: addDto.getdStoreDeficitInfoDto()){
                String companyId=infoDto.getMid();
                addDeficitDetail=dStoreDeficitDao.addDeficitDetail(companyId,dealerId,uuid,infoDto);
                if(addDeficitDetail<1){
                    throw new InsertInnerErrorException("新增报亏商品失败");
                }
            }
            return addDeficitDetail;

        }
        return addDeficitBase;
    }

    @Override
    public List<DStoreDeficitInfo> findDeficitList( String dealerId, DStoreDeficitListDto dStoreDeficitListDto) {
        List<DStoreDeficitInfo> info= dStoreDeficitDao.findDeficitList(dealerId,dStoreDeficitListDto);
        SimpleDateFormat date=new SimpleDateFormat("yyyy-MM-dd");
        for(DStoreDeficitInfo dStoreDeficitInfo:info){
            if(dStoreDeficitInfo.getCreateTime()!=null){
                dStoreDeficitInfo.setCreateTimes(date.format(dStoreDeficitInfo.getCreateTime()));
            }
            if(dStoreDeficitInfo.getConfirmTime()!=null){
                dStoreDeficitInfo.setConfirmTimes(date.format(dStoreDeficitInfo.getConfirmTime()));
            }
        }
        return info;
    }

    @Override
    public int findDeficitListCount( String dealerId, DStoreDeficitListDto dStoreDeficitListDto) {
        return dStoreDeficitDao.findDealerDeficitListCount(dealerId,dStoreDeficitListDto);
    }

    @Override
    public DStoreDeficitInfoDetail findDetail(String dealerId, String deficitId) {
        DStoreDeficitInfoDetail dStoreDeficitInfoDetail = dStoreDeficitDao.findDealerDeficitInfo(dealerId,deficitId);
            for(DStoreDeficitInfoList ds : dStoreDeficitInfoDetail.getdStoreDeficitInfoList()){
                String mid = ds.getMid();
                if(ds.getMid().equals("") || ds.getMid().equals("0")){
                    ds.setmName("自营");
                }else{
                    DStoreDeficitInfoList name = dStoreDeficitDao.findJXSname(ds.getMid());
                    if(name == null){
                        ds.setmName("自营");
                    }else{
                        ds.setmName(name.getmName());
                    }
                }
            }
        return dStoreDeficitInfoDetail;
    }

    @Override
    @Transactional
    public int editDStoreDeficitConfirm( String dealerId,String operatorId, String uuid) {
        int addSurplusBase = dStoreDeficitDao.editDStoreDeficitConfirm(dealerId,operatorId,uuid);
        int no = 0;
        float kk;
        float rainNum;
        if(addSurplusBase>0){
            DStoreDeficitAddDto list = dStoreDeficitDao.findData(dealerId,uuid);
            int success = 0;
            for(DStoreDeficitInfoDto infoDto: list.getdStoreDeficitInfoDto()){
                StockDetailDeficit num = dStoreDeficitDao.findProductStock(dealerId,list.getStoreId(),infoDto.getProductId(),infoDto.getMid());
                if(num == null){
                    no = 1;
                    rainNum = infoDto.getNum().floatValue();
                }else{
                    no = num.getNo() + 1;
                    kk = infoDto.getNum().floatValue();
                    if((num.getRemainNum() - kk) > 0){
                        rainNum = num.getRemainNum() - kk;
                    }else{
                        throw new InsertInnerErrorException(infoDto.getProName()+" 库存数量不足,失败");
                    }
                }
                success = dStoreDeficitDao.addProDuctStock(dealerId,list.getStoreId(),infoDto.getProductId(),no,uuid,infoDto.getNo(),infoDto.getNum(),rainNum,infoDto.getMid());
                if(success < 1){
                    throw new InsertInnerErrorException("新增库存明细账失败");
                }
            }
            return success;
        }else{
            throw new InsertInnerErrorException("新增库存明细账失败");
        }
    }

    @Override
    public int deleteDStoreDeficit( String dealerId, String uuid) {
        return dStoreDeficitDao.deleteDStoreDeficit( dealerId, uuid);
    }

    @Override
    public int editDeficitBase( String dealerId, DStoreDeficitEditDto editDto) {
        int editDeficitBase=dStoreDeficitDao.editDeficitBase(dealerId,editDto);
        if(editDeficitBase>0){
            String uuid=editDto.getUuid();
            int addDeficitDetail=0;
            int delete=dStoreDeficitDao.deleteInfo(dealerId,uuid);
            if(delete>0){
                for(DStoreDeficitInfoDto infoDto: editDto.getdStoreDeficitInfoDto()){
                    addDeficitDetail=dStoreDeficitDao.addDeficitDetail(infoDto.getMid(),dealerId,uuid,infoDto);
                    if(addDeficitDetail<1){
                        throw new InsertInnerErrorException("新增报亏商品失败");
                    }
                }
            }
            return addDeficitDetail;

        }
        return editDeficitBase;
    }
}
